/**
 * @(#)ApkSimulatorTimerServiceImpl.java 2017年3月25日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import org.jsbd.boss.domian.channel.ApkSimulator;
import org.jsbd.boss.domian.channel.SimulatorSysVer;
import org.jsbd.boss.service.ISimulatorSysVerService;
import org.jsbd.boss.service.ITimerCacheService;
import org.jsbd.boss.service.SimulatorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 更新apk可用模拟器
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service("apkSimulatorTimerService")
public class ApkSimulatorTimerServiceImpl implements ITimerCacheService {
	public static final Logger LOGGER = LoggerFactory.getLogger(ApkSimulatorTimerServiceImpl.class);

	@Resource
	private SimulatorManager simulatorManager;

	@Resource
	private ISimulatorSysVerService simulatorSysVerService;


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.service.ITimerCacheService#updateCache()
	 */
	@Override
	public void updateCache() {
		Map<String, Integer> simulatorOfVerMap = simulatorManager.getSimulatorOfVer();
		List<SimulatorSysVer> allSysVers = simulatorSysVerService.selectAllSimulatorSysVer();
		for (SimulatorSysVer simulatorVer : allSysVers) {
			String ver = simulatorVer.getVer();
			Integer verSimulatorNum = simulatorOfVerMap.get(ver);
			if (verSimulatorNum == null) {
				verSimulatorNum = 0;
			}
			updateApkSimulator(ver, verSimulatorNum);
		}
	}

	/**
	 * 
	 * updateApkSimulator:更新apk可用模拟器数量
	 * 
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @param ver
	 * @param verSimulatorNum
	 */
	private void updateApkSimulator(String ver, int verSimulatorNum) {
		ApkSimulator apkSimulator = new ApkSimulator();
		apkSimulator.setSysVer(ver);
		apkSimulator.setCount((double) verSimulatorNum);
		simulatorManager.addVerCanUseSimulator(apkSimulator);
	}



}
